<template>
  <div class="widget_settings_pro">
    <ele-form-section
        v-model="formData"
        :request-fn="handleSubmit"
        :sections="sections"
        @request-success="handleSuccess"
    >
      <template v-slot:collection="{ desc, data, field, formData }">
        <div class="editForm">
          <div class="form-item inline">
            <div class="product-select" v-if="formData.collection && formData.collection.id">
              <div class="selected">
                <span class="value">{{ formData.collection.name }}</span>
                <div class="icons">
                  <i class="el-icon-sort" @click="isOpen = true"></i>
                  <i class="icon_custom el-icon-delete" @click="delProduct"></i>
                </div>
              </div>
              <div class="select-bt">
                <router-link :to="{name: 'series-list'}" tag="a" class="link">管理系列</router-link>
              </div>
            </div>
            <el-button v-else type="primary" size="small" @click="isOpen = true"
                       style="width: 90%;margin: auto;display: block">添加系列
            </el-button>
          </div>
        </div>
      </template>
      <template v-slot:form-btn="{ btns }">
        <div class="rest-btn" @click="reset">
          <svg-icon icon-class="reset-settings"></svg-icon>
          重置配置
        </div>
      </template>
    </ele-form-section>
    <series-dialog :productDialogVisible="isOpen" @save="getSelectData" @close="isOpen = false"></series-dialog>
  </div>
</template>

<script>
import seriesDialog from '_c/Dialog/series_dialog';
import {debounce, deepClone} from '@/utils/utils';

export default {
    name: 'productsForm',
    components: {seriesDialog},
    props: {
        value: {
            type: Object,
            default: () => ({})
        }
    },
    watch: {
        value: {
            handler (newVal, oldVal) {
                this.formData = JSON.parse(newVal.styleJson);
            },
            immediate: false,
            deep: true
        },
        formData: {
            handler (newVal, oldVal) {
                this.pageCount++;
                if (this.pageCount !== 1) {
                    this.setData();
                }
            },
            immediate: false,
            deep: true
        }
    },
    data () {
        return {
            isOpen: false,
            pageCount: 1,
            formData: this.value ? JSON.parse(this.value.styleJson) : {},
            sections: [
                {
                    title: '内容',
                    formDesc: {
                        collection: {}
                    }
                },
                {
                    title: '设置',
                    formDesc: {
                        name: {
                            type: 'input',
                            label: '标题'
                        },
                        sub_name: {
                            type: 'ele-form-quill-edit',
                            label: '副标题'
                        },
                        'image_src': {
                            label: '选择图片',
                            type: 'ele-form-upload-plus-file',
                            attrs: {
                                title: '选择图片',
                                desc: '建议尺寸：1920x667px,高度可自适应'
                            }
                        },
                        'link_to_url': {
                            type: 'ele-form-custom-select',
                            label: '图片链接'
                        },
                        'line_number': {
                            type: 'slider',
                            label: () => { return `PC展示排数  ${this.formData['line_number']}`; },
                            attrs: {
                                min: 1,
                                max: 6
                            }
                        },
                        grid: {
                            type: 'slider',
                            label: () => { return `PC每排商品数 ${this.formData.grid}`; },
                            attrs: {
                                min: 2,
                                max: 6
                            }
                        },
                        'image_size': {
                            type: 'select',
                            label: '图片高度',
                            options: [
                                {text: '保留原尺寸', value: 'origin'},
                                {text: '大', value: 'large'},
                                {text: '中', value: 'medium'},
                                {text: '小', value: 'small'}
                            ]
                        },
                        'view_all_text': {
                            type: 'input',
                            label: '查看更多'
                        },
                        'view_all': {
                            type: 'switch',
                            break: true,
                            label: '显示更多按钮'
                        },
                        'no_padding': {
                            type: 'switch',
                            break: true,
                            label: '紧凑模式'
                        }
                    }
                },
                {
                    title: '间距留白',
                    formDesc: {
                        'module_layout': {
                            type: 'ele-spacing'
                        }
                    }
                }
            ]
        };
    },
    methods: {
        handleSubmit (data) {
            return Promise.resolve();
        },
        handleSuccess () {
            this.$message.success('创建成功');
        },
        reset () {
            this.pageCount = 0;
            if (!('module_layout' in this.value)) {
                this.$set(this.value, 'module_layout', {
                    left: 0,
                    top: 0,
                    right: 0,
                    bottom: 0
                });
            }
            this.$emit('resetData', this.value);
        },
        getSelectData (data) {
            this.isOpen = false;
            this.formData.collection = data;
        },
        delProduct () {
            this.formData.collection = null;
        },
        setData () {
            let data = deepClone(this.value);
            data.styleJson = JSON.stringify(this.formData);
            this.$store.commit('SET_PROJECT_DATA', data);
        }
    },
    mounted () {
        if (!('view_all_text' in this.formData)) {
            this.$set(this.formData, 'view_all_text', 'VIEW ALL');
        }
        if (!(('no_padding') in this.formData)) {
            this.$set(this.formData, 'no_padding', false);
        }
        if (!('module_layout' in this.formData)) {
            this.$set(this.formData, 'module_layout', {
                left: 0,
                top: 0,
                right: 0,
                bottom: 0
            });
        }
    }
};
</script>

<style scoped lang="scss">
.form-item-inline {
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 10px;
  padding-right: 10px;
  color: #606266;
  height: 32px;
  margin-bottom: 10px;
}

.editForm {
  margin-bottom: 10px;

  .form-item {
    margin-top: 10px;

    label {
      display: block;
      height: 32px;
      line-height: 32px;
    }

    &.inline {
      width: 242px;

      .selected {
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #eef0f2;
        padding: 0 10px;
        .icon {
          i:first-child {
            margin-right: 10px;
          }
        }
      }
    }
  }

  ::v-deep .el-collapse-item__content {
    border: 1px solid #d8deec;
    border-top: 0;
    padding-left: 10px;
  }
}

.addBtn {
  width: 100%;
}
.selected {
  width: 260px;
  background-color: #eef0f2;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .icon {
    i:first-child {
      margin-right: 10px;
    }
  }

  .value, .icons {
    display: flex;
    align-items: center;
  }

  .value {
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    white-space: nowrap;
    width: 160px;

    img {
      margin-right: 12px;
    }
  }

  .icons {
    padding-right: 12px;

    .el-icon-sort {
      margin-right: 12px;
    }
  }
}
.select-bt{
  .link{
    color: #1f7df8;
  }
  .desc{
    color: rgba(0, 0, 0, 0.65);
    line-height: 1;
    margin: 0;
    font-size: 12px;
  }
}
</style>
